package com.dpteam.dao.impl;

import com.dpteam.dao.ProductDao;
import com.dpteam.model.Category;
import com.dpteam.model.Product;
import com.dpteam.model.Provider;
import com.dpteam.util.Page;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xwh on 2016/1/8.
 */
@Repository
public class ProductDaoImpl extends BaseDaoImpl<Product> implements ProductDao {

    @Override
    public List<Product> findAllProduct() {
        String hql = "from Product order by id ";
        return find(hql);
    }

    @Override
    public List<Product> findAllProductPage(Page page) {
        String hql = "from Product order by id ";
        return find(hql, page);
    }

    @Override
    public Product findByBarcode(String barcode) {
        String hql = "from Product where barcode = ? ";
        return get(hql, barcode);
    }

    @Override
    public List<Product> findByProductName(String productName) {
        String hql = "from Product where productName like ? order by id";
        return find(hql, "%" + productName + "%");
    }

    @Override
    public List<Product> findLikeProductName(String productName, Page page) {
        String hql = "from Product where productName like ? order by id";
        return find(hql, page, "%" + productName + "%");
    }

    @Override
    public List<Product> findByCategory(int categoryId) {
        String hql = "from Product where categoryId = ? order by id ";
        return find(hql, categoryId);
    }

    @Override
    public List<Product> findByCategoryPage(int categoryId, Page page) {
        String hql = "from Product where categoryId = ? order by id ";
        return find(hql, page, categoryId);
    }

    @Override
    public List<Product> findByProvider(int providerId) {
        String hql = "from Product where providerId = ? order by id ";
        return find(hql, providerId);
    }

    @Override
    public List<Product> findByProviderPage(int providerId, Page page) {
        String hql = "from Product where providerId = ? order by id ";
        return find(hql, page, providerId);
    }

    @Override
    public void deleteById(int id) {
        Product product = get(Product.class, id);
        if (product != null) {
            delete(product);
        }
    }
}
